<template>
  <div>
    <Header active-index="2" />
    <div v-scroll-spy>
      <!--START HOME-->
      <section class="section bg-home home-half" id="home">
        <div class="bg-overlay"></div>
        <div class="container">
          <div class="row">
            <div class="col-lg-12 text-center">
              <Panorama />
              <br />
              <el-button type="primary" @click="to7777">立即查看</el-button>
            </div>
          </div>
        </div>
        <div class="wave-effect wave-anim">
          <div class="waves-shape shape-one">
            <div
              class="wave wave-one"
              :style="{
                'background-image':
                  'url(' +
                  require('@/assets/images/wave-shape/wave1.png') +
                  ')',
              }"
            ></div>
          </div>
          <div class="waves-shape shape-two">
            <div
              class="wave wave-two"
              :style="{
                'background-image':
                  'url(' +
                  require('@/assets/images/wave-shape/wave2.png') +
                  ')',
              }"
            ></div>
          </div>
          <div class="waves-shape shape-three">
            <div
              class="wave wave-three"
              :style="{
                'background-image':
                  'url(' +
                  require('@/assets/images/wave-shape/wave3.png') +
                  ')',
              }"
            ></div>
          </div>
        </div>
      </section>
      <!--END GET STARTED-->
    </div>
  </div>
</template>

<script>
import { releaseDeal } from "@/api/index";
import Header from "@/components/header/index";
import Footer from "@/components/footer/index";
import Panorama from "@/components/panorama";
import { Message } from "element-ui";
export default {
  components: {
    Header,
    Panorama,
    Footer,
  },
  data() {
    return {
      releaseVisible: false,
      IdRule: 1,
      typeSelect: "",
      ruleForm: {
        IdRule: "1",
      },
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      releaseDto: {
        input_type_id: "",
        trade_type: "",
        total_elec: "",
        start_time: "",
        end_time: undefined,
        price: "",
        date: "",
        elec_value: [],
        elec_upper: [],
        elec_lower: [],
      },
    };
  },
  methods: {
    to7777() {
      window.location.href = "http://43.138.104.96:7777/#/";
    },
    clearReleaseDto() {
      this.releaseDto = {
        input_type_id: "",
        trade_type: "",
        total_elec: "",
        start_time: "",
        end_time: undefined,
        price: "",
        date: "",
        elec_value: [],
        elec_upper: [],
        elec_lower: [],
      };
    },
    clearData() {
      this.releaseDto.elec_value = [];
      this.releaseDto.elec_lower = [];
      this.releaseDto.elec_upper = [];
    },
    agreeChange: function (val) {
      // console.log(val); // “判断题/选择题/多选题”
      this.typeSelect = val; // 选中的值=判断题/选择题/多选题
    },
    releaseDealHelp() {
      // 处理空数据情况
      if (
        this.releaseDto.input_type_id === "" ||
        this.releaseDto.trade_type === "" ||
        this.releaseDto.total_elec === "" ||
        this.releaseDto.price === "" ||
        this.releaseDto.start_time === "" ||
        this.releaseDto.end_time === undefined ||
        this.releaseDto.date === "" ||
        (this.releaseDto.input_type_id == 1 &&
          this.releaseDto.elec_value.length == 0) ||
        (this.releaseDto.input_type_id == 2 &&
          (this.releaseDto.elec_upper.length == 0 ||
            this.releaseDto.elec_lower.length == 0))
      ) {
        this.$message.error("数据不能为空！");
        return;
      }
      if (!/(^[1-9]\d*$)/.test(this.releaseDto.price)) {
        this.$message.error("价格格式不正确！");
        return;
      }
      if (!/(^[1-9]\d*$)/.test(this.releaseDto.total_elec)) {
        this.$message.error("数量格式不正确！");
        return;
      }
      // 处理交易时间段
      // 处理时间槽大于总电量情况
      if (this.releaseDto.input_type_id == 1) {
        var temp = parseInt(0);
        for (var i = 0; i < this.releaseDto.elec_value.length; i++) {
          temp += parseInt(this.releaseDto.elec_value[i]);
        }
        if (temp != this.releaseDto.total_elec) {
          this.$message.error("时间槽确定值和不等于总电量，请检查！");
          return;
        }
      }
      if (this.releaseDto.input_type_id == 2) {
        var tempLow = parseInt(0);
        var tempHigh = parseInt(0);
        for (var i = 0; i < this.releaseDto.elec_lower.length; i++) {
          if (this.releaseDto.elec_lower[i] > this.releaseDto.elec_upper[i]) {
            this.$message.error("时间槽下限应该小于上限！");
            return;
          }
          tempLow += parseInt(this.releaseDto.elec_lower[i]);
          tempHigh += parseInt(this.releaseDto.elec_upper[i]);
        }
        if (tempLow > this.releaseDto.total_elec) {
          this.$message.error("时间槽下限和大于总电量，请检查！");
          return;
        }
        if (tempHigh < this.releaseDto.total_elec) {
          this.$message.error("时间槽上限和小于总电量，请检查！");
          return;
        }
      }
      // 处理结束时间 开始时间情况
      if (this.releaseDto.start_time >= this.releaseDto.end_time) {
        this.$message.error("开始时间应早于结束时间");
        return;
      }
      console.log(this.releaseDto);
      const _this = this;
      releaseDeal(_this.releaseDto).then((res) => {
        _this.clearReleaseDto();
        _this.releaseVisible = false;
        Message.success("发布成功！");
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
};
</script>

<style>
.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}
.w200input .el-input,
.w200input input {
  width: 100px !important;
  margin: 0px 10px 5px 0px;
  display: inline-block;
}
</style>
